package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrType;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;

// Assuming PIID, Unsigned16, TSA, OAD, RSD, and RCSD are already defined in Java
// and have similar methods as in the TypeScript code provided.

public class ProxyGetRequestRecord extends AxdrType {
    private ByteBuffer dataCode = null;
    private PIID piid = null;
    private Unsigned16 waitTime = null;
    private TSA tsa = null;
    private OAD oad = null;
    private RSD rsd = null;
    private RCSD rcsd = null;

    public ProxyGetRequestRecord() {
    }

    public void setDataCode(ByteBuffer dataCode) {
        this.dataCode = dataCode;
    }

    public void setAll(PIID piid, Unsigned16 waitTime, TSA tsa, OAD oad, RSD rsd, RCSD rcsd) {
        this.piid = piid;
        this.waitTime = waitTime;
        this.tsa = tsa;
        this.oad = oad;
        this.rsd = rsd;
        this.rcsd = rcsd;
    }

    public void setPIID(PIID piid) {
        this.piid = piid;
    }

    public void setWaitTime(Unsigned16 waitTime) {
        this.waitTime = waitTime;
    }

    public void setTsa(TSA tsa) {
        this.tsa = tsa;
    }

    public void setOAD(OAD oad) {
        this.oad = oad;
    }

    public void setRSD(RSD rsd) {
        this.rsd = rsd;
    }

    public void setRCSD(RCSD rcsd) {
        this.rcsd = rcsd;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        this.piid = new PIID();
        this.piid.setNumBit(8);
        codeLength += this.piid.decode(input);
        this.waitTime = new Unsigned16();
        this.waitTime.setConst();
        codeLength += this.waitTime.decode(input);
        this.tsa = new TSA();
        codeLength += this.tsa.decode(input);
        this.oad = new OAD();
        codeLength += this.oad.decode(input);
        this.rsd = new RSD();
        codeLength += this.rsd.decode(input);
        this.rcsd = new RCSD();
        codeLength += this.rcsd.decode(input);
        return codeLength;
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        int codeLength;
        if (this.dataCode != null) {
            codeLength = this.dataCode.remaining();
            while (this.dataCode.hasRemaining()) {
                output.write(this.dataCode.get());
            }
        } else {
            if (this.rcsd != null && this.rsd != null && this.oad != null && this.tsa != null && this.waitTime != null && this.piid != null) {
                codeLength = 0;
                codeLength += this.rcsd.encode(output);
                codeLength += this.rsd.encode(output);
                codeLength += this.oad.encode(output);
                codeLength += this.tsa.encode(output);
                codeLength += this.waitTime.encode(output);
                codeLength += this.piid.encode(output);
            } else {
                throw new RuntimeException("rcsd || rsd || oad || tsa || waitTime || piid is null");
            }
        }
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = ByteBuffer.wrap(revOStream.getArray());
    }

    @Override
    public String toString() {
        if (this.piid != null && this.waitTime != null && this.tsa != null && this.oad != null && this.rsd != null && this.rcsd != null) {
            return "sequence: {" + "piid: " + this.piid + ", waitTime: " + this.waitTime + ", tsa: " + this.tsa + ", oad: " + this.oad + ", rsd: " + this.rsd + ", rcsd: " + this.rcsd + "}";
        } else {
            return "rcsd || rsd || oad || tsa || waitTime || piid is null";
        }
    }
}



